<template>
  <div :class="className" :style="{ height: height, width: width }"/>
</template>

<script>
import * as echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartdata: {
      type: Array
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      var that = this
      var data = []
      for (var i = 0; i < that.chartdata.length - 1; i++) {
        for (var j = 0; j < that.chartdata.length - 1; j++) {
          if (that.chartdata[j].cnt < that.chartdata[j + 1].cnt) {
            var temp = that.chartdata[j + 1]
            that.chartdata[j + 1] = that.chartdata[j]
            that.chartdata[j] = temp
          }
        }
      }
      var limitvalue = 0
      for (var i = 0; i < that.chartdata.length; i++) {
        if (i <= 8) {
          data.push({
            name: that.chartdata[i].dicType,
            value: that.chartdata[i].cnt
          })
        } else {
          limitvalue = limitvalue + parseInt(that.chartdata[i].cnt)
        }
      }
      if (that.chartdata.length >= 10) {
        data.push({ name: '其它', value: limitvalue })
      }

      this.initChart(data)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(data) {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '安全隐患原因分类统计分析',
          left: 'left',
          padding: [
            10, // 上
            10, // 右
            10, // 下
            10 // 左
          ],
          textStyle: { color: '#606266', fontSize: '14',fontWeight:'bolder' }
        },
        tooltip: {
          trigger: 'item',
          textStyle: { color: 'rgb(255,255,255)' }
        },
        legend: {
          icon: 'circle',
          orient: 'vertical',
          top: '50px',
          left: '70%',
          textStyle: { color: "#606266" },
        },
        series: [
          {
            name: '隐患数量',
            type: 'pie',
            radius: ['30%', '70%'],
            center: ['36%', '50%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 5
            },
            label: {
              show: false
            },
            emphasis: {
              /*   label: {
                  show: false,
                  fontSize: "24",
                  fontWeight: "bold",
                }, */
              focus: 'self'
            },
            labelLine: {
              show: true
            },
            data: data
          }
        ]
      })
    }
  }
}
</script>
